package com.junlinpro.utils;


import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import com.junlinpro.entity.MessageEntity;


public class HBaseUtil {	
	
    //表名 soudata_timeorder (包含声音）
    private static final byte[] TABLE_soudata_timeorder = Bytes.toBytes("origin_sound_DB:soudata_timeorder");
   
    private static Connection connection;

    static {
        Configuration configuration = new Configuration();

        //指定zk的链接地址
        configuration.set("hbase.zookeeper.quorum", "10.1.52.3:2181,10.1.52.4:2181");
        configuration.set("hbase.client.keyvalue.maxsize","52428800");

        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
                * 采集声音到content表中
     * A）行健编码：时间（毫秒）_通道ID
     * B）故障类型为N，表示正常状态；
     * C）采集者如果为人工（手持式），为采集人ID；如果为自动采集，为采集卡。
     */
    public static void saveSoudataTimeorder(MessageEntity msgEntity, String rowkey, byte[] opusByte) throws IOException {        

        Table table_content = connection.getTable(TableName.valueOf(TABLE_soudata_timeorder));
         
        Put put = new Put(rowkey.getBytes());
        //rowkey  family  column   value   ->  cell
        put.addColumn("collector_info".getBytes(), "adapter_id".getBytes(), Bytes.toBytes(msgEntity.getCardId())); //采集卡ID
        put.addColumn("collector_info".getBytes(), "channel_id".getBytes(), Bytes.toBytes(msgEntity.getChannelId())); //通道ID
        put.addColumn("collector_info".getBytes(), "collector_id".getBytes(),  Bytes.toBytes(msgEntity.getCardType())); //采集者(0:固定 1:手持)
        put.addColumn("time_info".getBytes(), "start_time".getBytes(), Bytes.toBytes(msgEntity.getOccurTime())); //开始时间
        
        put.addColumn("status_info".getBytes(), "fault_type".getBytes(), Bytes.toBytes("N")); //故障类型
        //其他列...

        put.addColumn("soudata".getBytes(), "soudData".getBytes(), opusByte); //声音数据
        table_content.put(put);
        
        channelTndexSoudata(msgEntity.getCardId(),msgEntity.getChannelId(),System.currentTimeMillis(),rowkey);
        
    }
    
    /**
             *    以通道ID为行健的数据表（不包含声音数据）
     */
    private static void channelTndexSoudata(int cardId, byte channelId, long ts, String dataRowkey) {
        try { 
            Table channelIndexSoudata = connection.getTable(TableName.valueOf("origin_sound_DB:channel_index_soudata"));

            //行健编码：时间（毫秒）_通道ID
            String rowkey = cardId+"_"+channelId + "_" + ts;
            Put put = new Put(rowkey.getBytes());
            
            put.addColumn("rowkey_T".getBytes(), "index".getBytes(), dataRowkey.getBytes());

            channelIndexSoudata.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
